<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:da="livcos.org/ns/cosmos/data-access"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c da f xs">

  <xsl:import href="../style/base.xsl" />
  <xsl:import href="../style/web.xsl" />
  
  <xsl:param name="objId"/>

  <xsl:variable name="browseUri" select="'explore.view'"/>

  <xsl:variable name="adminObjId">
    <xsl:value-of select="$objId"/>
    <xsl:if test="normalize-space($objId) = ''">/cosmos</xsl:if>
  </xsl:variable>

  <xsl:template match="/">
  	<xsl:variable name="uri" select="concat($adminObjId,'.meta2')"/>
  	<xsl:choose>
  		<xsl:when test="doc-available($uri)">
  			<xsl:apply-templates select="document($uri)/*"/>
  		</xsl:when>
  		<xsl:otherwise>
  			<xsl:apply-templates select="document('/cosmos.meta2')/*"/>
  		</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>

  <xsl:template match="/c:*">
    <xsl:variable name="objectId" select="f:object-id(.)"/>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Explore <xsl:value-of select="$objectId"/></title>
        <link href="../css/styles.css" rel="stylesheet" type="text/css" />
        <link href="../css/explore.css" rel="stylesheet" type="text/css" />
      </head>
      <body>
        <table>
          <thead>
            <tr>
              <td colspan="5" class="path">
                <xsl:call-template name="buildNavPath">
                  <xsl:with-param name="path" select="$objectId"/>
                </xsl:call-template>
              </td>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="c:*"/>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template name="buildNavPath">
    <xsl:param name="path"/>
    <xsl:param name="base" select="''"/>
    <xsl:choose>
      <xsl:when test="starts-with($path,'/')">
        <a href="{$browseUri}">/</a>
        <xsl:call-template name="buildNavPath">
          <xsl:with-param name="path" select="substring-after($path,'/')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($path,'/')">
        <xsl:if test="$base != ''">/</xsl:if>
        <xsl:variable name="name" select="substring-before($path,'/')"/>
        <a href="{$browseUri}?objId={$base}/{$name}"><xsl:value-of select="$name"/></a>
        <xsl:call-template name="buildNavPath">
          <xsl:with-param name="path" select="substring-after($path,'/')"/>
          <xsl:with-param name="base" select="concat($base, '/', $name)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$base != ''">/</xsl:if>
        <b><xsl:value-of select="$path"/></b>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="c:*">
    <xsl:variable name="id" select="f:object-id(.)"/>
    <xsl:variable name="folder" select="c:* or da:folder"/>
    <xsl:variable name="contentAccess" select="da:*[contains(@scopes, 'content')]"/>
    <xsl:variable name="fileExt" select="if ($contentAccess/@ext) then $contentAccess/@ext else 'view'"/>
    <xsl:variable name="modified" select="max(da:*/xs:dateTime(@modified))"/>
    <tr id="{$id}.meta2">
      <xsl:if test="(position() mod 2) = 0">
        <xsl:attribute name="class">even</xsl:attribute>
      </xsl:if>
      <td class="col1">
        <img src="../img/{if ($folder) then 'folder.png' else 'readMode.png'}"/>
      </td>
      <td class="col2">
        <a>
          <xsl:attribute name="href">
            <xsl:choose>
              <xsl:when test="$folder">
                <xsl:value-of select="$browseUri"/>?objId=<xsl:value-of select="$id"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$toRootPath"/>
                <xsl:value-of select="$id"/>
                <xsl:if test="$fileExt != ''">.<xsl:value-of select="$fileExt"/></xsl:if>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:value-of select="name(.)"/>
        </a>
      </td>
      <td class="col3">
        <xsl:value-of select="$contentAccess/@size"/>
        <xsl:if test="not($contentAccess)">&#160;</xsl:if>
      </td>
      <td class="col4">
        <xsl:value-of select="$contentAccess/@content-type"/>
        <xsl:if test="not($contentAccess/@content-type) and ($contentAccess/@ext != '')">
          <xsl:text>'</xsl:text>
          <xsl:value-of select="$contentAccess/@ext"/>
          <xsl:text>' File</xsl:text>
        </xsl:if>
      </td>
      <td class="col5">
        <xsl:value-of select="format-dateTime($modified, '[D01].[M01].[Y0001] [H01]:[m01]:[s01]')"/>
      </td>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>
